import { Component, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd/message';
import { groupBy, formate, paralleTask } from '../../../utils/index'
export interface TreeNodeInterface {
  key: string;
  name: string;
  age?: number;
  level?: number;
  expand?: boolean;
  address?: string;
  children?: TreeNodeInterface[];
  parent?: TreeNodeInterface;
}
@Component({
  selector: 'app-department',
  templateUrl: './department.component.html',
  styleUrls: ['./department.component.scss']
})

export class DepartmentComponent implements OnInit {
  @ViewChild('handleOpen')
  validateForm!: FormGroup
  constructor(private nzMessageService: NzMessageService,  private fb: FormBuilder) { }
  people: any = [
    {name: 'lp', age: 28, sex: 'nan'},
    {name: 'lp1', age: 21, sex: 'nv'},
    {name: 'lp', age: 28, sex: 'nan'},
    {name: 'lp1', age: 212, sex: 'nv'},
    {name: 'lp', age: 285, sex: 'nan'},
    {name: 'lp1', age: 231, sex: 'nv'},
  ]
  
  listOfMapData = [
    {
      key: `1`,
      name: '华南分布',
      children: [
        {
          key: `1-1`,
          name: '研发部',
        },
        {
          key: `1-2`,
          name: '人事部',
        },
        {
          key: `1-3`,
          name: '行政部',
        }
      ]
    }
  ];
  mapOfExpandedData: { [key: string]: TreeNodeInterface[] } = {};
  // groupBy(this.people, 'sex')

  collapse(array: TreeNodeInterface[], data: TreeNodeInterface, $event: boolean): void {
    if (!$event) {
      if (data.children) {
        data.children.forEach(d => {
          const target = array.find(a => a.key === d.key)!;
          target.expand = false;
          this.collapse(array, target, false);
        });
      } else {
        return;
      }
    }
  }
  convertTreeToList(root: TreeNodeInterface): TreeNodeInterface[] {
    const stack: TreeNodeInterface[] = [];
    const array: TreeNodeInterface[] = [];
    const hashMap = {};
    stack.push({ ...root, level: 0, expand: false });

    while (stack.length !== 0) {
      const node = stack.pop()!;
      this.visitNode(node, hashMap, array);
      if (node.children) {
        for (let i = node.children.length - 1; i >= 0; i--) {
          stack.push({ ...node.children[i], level: node.level! + 1, expand: false, parent: node });
        }
      }
    }

    return array;
  }

  visitNode(node: TreeNodeInterface, hashMap: { [key: string]: boolean }, array: TreeNodeInterface[]): void {
    if (!hashMap[node.key]) {
      hashMap[node.key] = true;
      array.push(node);
    }
  }

  // 删除取消和确认
  cancel(): void {
    this.nzMessageService.info('click cancel');
  }
  confirm(e: string): void {
    
    this.nzMessageService.success('success');

  }
  handleRest() {
    this.validateForm.reset()
  }
  handleSearch() {
    if (this.validateForm.valid) {
      console.log('submit', this.validateForm.value);
      // this.isVisible = false
      // this.isOkLoading = false
    } else {
      Object.values(this.validateForm.controls).forEach(control => {
        if (control.invalid) {
          control.markAsDirty();
          control.updateValueAndValidity({ onlySelf: true });
        }
      });
    }
  }
  ngOnInit(): void {
    console.log(groupBy(this.people, (item:any)=> `${item.sex}`), formate(new Date(), 'yyyy/MM/dd', true));
    this.listOfMapData.forEach(item => {
      this.mapOfExpandedData[item.key] = this.convertTreeToList(item);
    });
    this.validateForm = this.fb.group({
      name: [null],
      status: [null],
    });
  }

}
